package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import android.app.DatePickerDialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;

import android.widget.DatePicker;
import android.widget.TextView;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;
import android.widget.Spinner;
import android.widget.ArrayAdapter;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class PlanActivity extends AppCompatActivity {

    EditText editTextStartDate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plan);


        TextView timeTextView = findViewById(R.id.timeTextView);
        Spinner weekdaySpinner = findViewById(R.id.weekdaySpinner);

        editTextStartDate = findViewById(R.id.editTextStartDate);
        editTextStartDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog(PlanActivity.this);
            }
        });

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_home) {
                startActivity(new Intent(PlanActivity.this, HomeActivity.class));
                return true;
            } else if (itemId == R.id.navigation_focus) {
                startActivity(new Intent(PlanActivity.this, FocusActivity.class));
                return true;
            } else if (itemId == R.id.navigation_mine) {
                startActivity(new Intent(PlanActivity.this, MineActivity.class));
                return true;
            }
            return false;
        });


        // 格式化当前时间并设置给 TextView 控件
        SimpleDateFormat dateFormat = new SimpleDateFormat("'今天' HH:mm", Locale.getDefault());
        String currentTime = dateFormat.format(new Date());
        timeTextView.setText(currentTime);


        // 创建适配器，并为 Spinner 设置适配器
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.weekday_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        weekdaySpinner.setAdapter(adapter);

        // 获取字符串数组
        String[] weekdays = getResources().getStringArray(R.array.weekday_array);

// 创建适配器
        ArrayAdapter<String> adaptertwo = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, weekdays);

// 设置下拉菜单样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// 设置适配器
        Spinner spinner = findViewById(R.id.weekdaySpinner);
        spinner.setAdapter(adaptertwo);

    }

    private void showDatePickerDialog(Context context) {
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(context,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 将选定的日期显示在 EditText 控件中
                        editTextStartDate.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                    }
                }, year, month, dayOfMonth);

        datePickerDialog.show();
    }



}